package com.hskn.hss.module.gridlevel.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.gridlevel.entity.GridLevel;
import com.hskn.hss.module.gridlevel.mapper.GridLevelMapper;
import com.hskn.hss.module.gridlevel.service.IGridLevelService;
import com.hskn.hss.module.userdepart.service.ISysUserDepartService;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-08
 */
@Service
public class GridLevelServiceImpl extends ServiceImpl<GridLevelMapper, GridLevel> implements IGridLevelService {

    @Resource
    GridLevelMapper gridLevelMapper;

    @Autowired
    ISysUserDepartService sysUserDepartService;

    @Override
    public List<GridLevel> mList(JSONObject params) throws HssException {
        return list();
    }

    @Override
    public void mUpdate(JSONObject params) throws HssException {
        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("levelId"))) {
            throw new HssException("参数错误");
        }
        GridLevel gridLevel = JSON.parseObject(params.toJSONString(), GridLevel.class);
        gridLevelMapper.updateById(gridLevel);
    }

    @Override
    public void add(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        GridLevel gridLevel = JSON.parseObject(params.toJSONString(), GridLevel.class);
        gridLevel.setCreateUid(uid);
        gridLevel.setCreateDepid(sysUserDepartService.getDeptidByUid(uid));
        gridLevelMapper.insert(gridLevel);
    }

    @Override
    public void del(JSONObject params) throws HssException {
        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("levelIds"))) {
            throw new HssException("参数错误");
        }
        String idStr = params.getString("levelIds");
        String[] ids = idStr.split(",");
        gridLevelMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public GridLevel detail(JSONObject params) throws HssException {
        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("levelId"))) {
            throw new HssException("参数错误");
        }
        GridLevel gridLevel = gridLevelMapper.selectById(params.getString("levelId"));
        if (StringUtils.isEmpty(gridLevel)) {
            throw new HssException("数据不存在");
        }
        return gridLevel;
    }
}
